<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>歌曲管理</el-breadcrumb-item>
      <el-breadcrumb-item>专辑列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!--搜索区域-->
      <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryinfo.query" clearable @keydown.enter.native="getSpecial"
                    class="input-with-select" @clear="getSpecial">
            <el-button slot="append" icon="el-icon-search" @click="getSpecial"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="openDialog">新增专辑</el-button>
        </el-col>
      </el-row>
      <!--显示区域-->
      <el-row>
        <el-table :data="specials" border style="width: 100%" stripe>
          <el-table-column type="index" label="序号" width="60px"></el-table-column>
          <el-table-column prop="name" label="专辑名称"></el-table-column>
          <el-table-column prop="libretto" label="专辑简介"></el-table-column>
          <el-table-column label="图片"></el-table-column>
          <el-table-column label="操作" width="150px">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="openDialog(scope.row.id)">修改</el-button>
              <el-button size="mini" type="danger" @click="deleteSpecial(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!--分页区域-->
      <el-row>
        <el-pagination layout="total, prev, pager, next" @current-change="handleCurrentChange"
                       :total="total">
        </el-pagination>
      </el-row>
    </el-card>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible"
               :close-on-click-modal="false" @close="close">
      <el-form :model="special" label-width="80px" ref="specialrRef">
        <el-form-item label="专辑名">
          <el-input v-model="special.name"></el-input>
        </el-form-item>
        <el-form-item label="专辑介绍">
          <el-input type="textarea" v-model="special.libretto"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryinfo: {
        page: 1,
        size: 10,
        query: '',
        album: 1
      },
      specials: [],
      total: -1,
      dialogFormVisible: false,
      dialogTitle: '',
      special: {
        name: '',
        libretto: '',
        parentId: 0
      }
    }
  },
  methods: {
    // 删除专辑
    deleteSpecial (id) {
      this.$confirm('是否删除专辑', '确认信息', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确认',
        cancelButtonText: '放弃'
      }).then(() => {
        this.$http.delete(`song/${id}`).then(res => {
          console.log(this.total % this.queryinfo.size)
          if (this.total % this.queryinfo.size === 1) {
            this.queryinfo.page = Math.floor(this.total / this.queryinfo.size)
            this.getSpecial()
          } else {
            this.getSpecial()
          }
          this.hite(res.data.code)
        })
      }).catch(action => {
        this.$message({
          type: 'info',
          message: '已取消删除',
          duration: 1000
        })
      })
    },
    submitForm () {
      // 修改
      if (!isNaN(this.id)) {
        this.$http.put(`song/${this.id}`, this.special).then(res => {
          this.getSpecial()
          this.id = 'id'
          this.hite(res.data.code)
        })
      }
      // 添加
      else {
        this.$http.post('song', this.special).then(res => {
          this.getSpecial()
          this.hite(res.data.code)
        })
      }
      this.dialogFormVisible = false
    },
    // 关闭dialog对话框
    close () {
      this.$refs.specialrRef.resetFields()
      this.special = { name: '', libretto: '' }
    },
    // 打开dialog对话框
    openDialog (id) {
      if (!isNaN(id)) {
        this.id = id
        this.dialogTitle = '修改专辑'
        this.$http.get(`song/${id}`).then(res => {
          this.special = res.data.data
        })
      } else {
        this.dialogTitle = '新增专辑'
      }
      this.dialogFormVisible = true
    },
    // 页码改变
    handleCurrentChange (val) {
      this.queryinfo.page = val
      this.getSpecial()
    },
    hite (code) {
      if (code === 10000) {
        this.$message.success({
          message: '成功',
          duration: 1000
        })
      } else {
        this.$message.error({
          message: '失败',
          duration: 1000
        })
      }
    },
    getSpecial () {
      this.$http.get(`song/${this.queryinfo.page}/${this.queryinfo.size}`, {
        params: { 'album': this.queryinfo.album, 'query': this.queryinfo.query }
      }).then(res => {
        this.specials = res.data.data.rows
        this.total = res.data.data.total
        // this.queryinfo.page = 1
      })
    }
  },
  created () {
    this.getSpecial()
  }
}
</script>

<style scoped>

</style>
